package com.heima.comment.service.impl;

import com.heima.apis.wemedia.IWmHandClient;
import com.heima.model.comment.dtos.CommentDto;
import com.heima.model.comment.dtos.CommentLikeDto;
import com.heima.model.comment.dtos.CommentSaveDto;
import com.heima.model.comment.pojos.CommentLike;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.comment.repository.CommentRepository;
import com.heima.comment.service.CommentService;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.utils.common.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.stereotype.Service;
import com.heima.model.comment.pojos.Comment;

import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private CommentRepository commentRepository;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IWmHandClient wmHandClient;

    //新增评论
    @Override
    public ResponseResult save(CommentSaveDto dto) {
        if (null == dto || dto.getContent() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //长度判断
        if (dto.getContent().length() > 140) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //文本垃圾检测
        if (!wmHandClient.handle(dto.getContent())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        Comment comment = new Comment();
        //设置文章id
        comment.setArticleId(dto.getArticleId());
        //设置用户id
        comment.setUserId(AppThreadLocalUtil.getUser().getId());
        //设置评论内容
        comment.setContent(dto.getContent());
        //设置评论点赞数量
        comment.setLikes(0);
        //设置回复
        comment.setReply(0);
        //创建修改时间
        comment.setOperation(1);
        comment.setCreatedTime(new Date().getTime());
        comment.setUpdatedTime(new Date().getTime());

        //存入DB数据库
        comment = commentRepository.save(comment);


        System.out.println("获取到保存的文章评论" + comment);
        return ResponseResult.okResult(comment);
    }



    //查询评论列表
    @Override
    public ResponseResult load(CommentDto dto) {

        Query query = new Query();
        query.addCriteria(Criteria.where("userId").is(AppThreadLocalUtil.getUser().getId()))
                .addCriteria(Criteria.where("createdTime").lt(dto.getMinDate()))
                .addCriteria(Criteria.where("articleId").is(dto.getArticleId()))
                .with(Sort.by(Sort.Direction.DESC, "createdTime"));
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        System.out.println(commentList);
        return ResponseResult.okResult(commentList);
    }

    //评论点赞
    @Override
    public ResponseResult like(CommentLikeDto dto) {

        Query query=new Query();
        query.addCriteria(Criteria.where("userId").is(AppThreadLocalUtil.getUser().getId()))
                .addCriteria(Criteria.where("_id").is(dto.getCommentId()));
        Comment comment = mongoTemplate.findOne(query, Comment.class);
        if (null==comment||null==comment.getLikes()){
          comment.setLikes(0);
        }

        if (dto.getOperation() == 0) {
                comment.setOperation(1);
                comment.setLikes(comment.getLikes()+1);
        } else {
            comment.setOperation(0);
            comment.setLikes(comment.getLikes()-1);
        }

        return ResponseResult.okResult(comment.getLikes());
    }
}
